package com.erp.admin.business.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.erp.base.base.BaseController;
import com.erp.base.business.entity.Server;
import com.erp.base.business.service.ServerService;
import com.erp.base.common.utils.R;

/**
 * 行业服务表
 * 
 * @author wanghw
 * @email whongwei_yz@sina.com
 * @date 2018-10-22 11:31:34
 */

@Controller
@RequestMapping("/business/server")
public class ServerController extends BaseController {
	@Autowired
	private ServerService serverService;

	@GetMapping()
	@RequiresPermissions("business:server:server")
	String Server() {
		return "business/server/server";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("business:Server:server")
	public List<Server> list(HttpServletRequest request, HttpServletResponse response) {
		// 查询列表数据
		/*Page<Server> page = Page.bootstrapPage(request, response);
		page = serverService.findPage(page, server);*/
		return serverService.findList(new Server());
	}
	@GetMapping("/add/{pId}")
	@RequiresPermissions("business:server:add")
	String add(@PathVariable("pId") Long pId, Model model) {
		if (pId == 0) {
			model.addAttribute("pName", "顶层服务");
		} else {
			model.addAttribute("pName", serverService.get(pId).getTitle());
		}
		return "business/server/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("business:server:edit")
	String edit(@PathVariable("id") Long id, Model model) {
		Server server = serverService.get(id);
		model.addAttribute("server", server);
		Long parentId = server.getParentId();
		model.addAttribute("parentServer", parentId.compareTo(0l)==0?"顶层服务":serverService.get(parentId).getTitle());
		return "business/server/edit";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("business:server:add")
	public R save(Server server, Model model) {
		if (!beanValidator(model, server)) {
			return R.error((String) model.asMap().get("message"));
		}
		try {
			serverService.save(server);
			return R.ok();
		} catch (Exception e) {
			logger.error("保存失败", e);
			return R.error();
		}
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("business:server:edit")
	public R update(Server server, Model model) {
		if (!beanValidator(model, server)) {
			return R.error((String) model.asMap().get("message"));
		}
		try {
			serverService.save(server);
			return R.ok();
		} catch (Exception e) {
			logger.error("保存失败", e);
			return R.error();
		}
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ResponseBody
	@RequiresPermissions("business:server:remove")
	public R remove(Long id) {
		try {
			Server parentServer=new Server();
			parentServer.setParentId(id);
			List<Server> list = serverService.findList(parentServer);
			if(list==null||list.isEmpty()){
				serverService.delete(id);
				return R.ok();
			}else{
				return R.error("有下级，不能删除！");
			}
			
		} catch (Exception e) {
			logger.error("删除失败， id:" + id, e);
			return R.error();
		}
	}

	/**
	 * 删除
	 */
	@PostMapping("/batchRemove")
	@ResponseBody
	@RequiresPermissions("business:server:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids) {
		try {
			serverService.deleteBatch(Arrays.asList(ids));
			return R.ok();
		} catch (Exception e) {
			logger.error("删除失败，ids:" + ids, e);
			return R.error();
		}

	}

}
